package com.grwx.omc.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grwx.omc.admin.entity.SysDevice;
import com.grwx.omc.admin.service.ISysDeviceService;
import com.grwx.omc.constant.Constant;
import com.grwx.omc.entity.ResponseData;
import com.grwx.omc.exception.ValidationException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Description: 设备管理
 * @Author: ManJiang
 * @Date: 2020/5/14
 */
@RestController
@RequestMapping(value = "sys_device")
public class SysDeviceController {
    @Autowired
    private ISysDeviceService deviceService;


    @ApiOperation("设备查询分页")
    @PostMapping(value = "/search/{page}/{size}")
    public ResponseData page(@RequestBody SysDevice sysDevice,@PathVariable("page") int page, @PathVariable("size") int size){
        Page<SysDevice> sysDevicePage = new Page<>(page,size);

        IPage<SysDevice> result = deviceService.page(sysDevicePage,sysDevice);

        return ResponseData.success(result);
    }

    @ApiOperation("设备新增")
    @PostMapping()
    public ResponseData insert(@RequestBody @Valid SysDevice sysDevice){
        if (null == sysDevice) {
            throw new ValidationException(Constant.MsgCode.EMPTY_PARAM);
        }

        deviceService.addOrUpdate(sysDevice);
        return ResponseData.success(Constant.MsgCode.INSERT_SUCCESS);
    }

    @ApiOperation("设备修改")
    @PutMapping()
    public ResponseData update(@RequestBody @Valid SysDevice sysDevice){
        if (null == sysDevice) {
            throw new ValidationException(Constant.MsgCode.EMPTY_PARAM);
        }

        deviceService.addOrUpdate(sysDevice);
        return ResponseData.success(Constant.MsgCode.UPDATE_SUCCESS);
    }

    @ApiOperation("设备删除")
    @DeleteMapping()
    public ResponseData delete(Long[] ids){
        if (null==ids || ids.length==0){
            throw new ValidationException(Constant.MsgCode.EMPTY_DELETE);
        }

        deviceService.del(ids);
        return ResponseData.success(Constant.MsgCode.DELETE_SUCCESS);
    }
}
